﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AntiObesity.DatabaseAccess;

namespace AntiObesity.Base.Managers
{
    public static partial class FoodManager
    {
        public static List<Food> FindByNameSimilarity(String partOfName)
        {
            return FindByNameSimilarity(partOfName, 20);
        }

        public static List<Food> FindByNameSimilarity(String partOfName, int noOfItems)
        {
            var result = new List<Food>();
            using (var conn = DB.Instance.Current.GetConnection())
            {
                var reader = DB.Instance.Current.ExecuteForReader(conn, String.Format("SELECT TOP ({0}) * FROM [Foods] WHERE CHARINDEX('{1}',[name])>0 ORDER BY DATALENGTH([name]),[name] ", noOfItems, partOfName.Replace("'", "''")));
                while (reader.Read())
                {
                    result.Add(CreateFood(reader));
                }
            }
            return result;
        }

        public static List<Food> GetPopularFoods(Guid categoryId)
        {
            // currently it is only a random selection for test purposes
            var result = new List<Food>();
            using (var conn = DB.Instance.Current.GetConnection())
            {
                var reader = DB.Instance.Current.ExecuteForReader(conn, String.Format("SELECT TOP 3 * FROM [Foods] WHERE [ID] LIKE '%{0}%' AND categoryId ='{1}' ORDER BY [name]", "8", categoryId));
                while (reader.Read())
                {
                    result.Add(CreateFood(reader));
                }
            }
            return result;
        }
    }
}
